package com.zhongshengnetwork.rightbe.common.TextUtil;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Paint;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;

import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.ScreenUtils;

import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FontUitls {

    public static int getFontWidth(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        Paint.FontMetrics fm = paint.getFontMetrics();

        return (int) Math.ceil(fm.descent - fm.ascent);
    }

    public static int getFontHeight(float fontSize) {
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        Paint.FontMetrics fm = paint.getFontMetrics();

        return (int) Math.ceil(fm.bottom - fm.top);
    }

    public static int getTextMaxLine(int height, Context context){

        if (height >= 2160){
            return 7;
        }else if (height >= 1920){
            return 6;
        }
        return 5;

    }

    //获取是否存在NavigationBar
    public static boolean checkDeviceHasNavigationBar(Context context) {
        boolean hasNavigationBar = false;
        Resources rs = context.getResources();
        int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id);
        }
        try {
            Class systemPropertiesClass = Class.forName("android.os.SystemProperties");
            Method m = systemPropertiesClass.getMethod("get", String.class);
            String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys");
            if ("1".equals(navBarOverride)) {
                //不存在虚拟按键
                hasNavigationBar = false;
            } else if ("0".equals(navBarOverride)) {
                //存在虚拟按键
                hasNavigationBar = true;
            }
        } catch (Exception e) {
            return false;
        }
        return hasNavigationBar;
    }

    public static boolean isPortDisplay(String title){

        /*
        if (CommonUtils.isEmpty(title)){
            return false;
        }

        if (title.contains("——")){
            return false;
        }
        if (title.contains("《") || title.contains("》")){
            return false;
        }
        if (title.contains("……")){
            return false;
        }

        if(title.contains("（") || title.contains("）") || title.contains("(") || title.contains(")")){
            return false;
        }

        if (title.contains("【") || title.contains("】")){
            return false;
        }

        if (title.contains("{") || title.contains("}")){
            return false;
        }

        if (title.contains("<") || title.contains(">")){
            return false;
        }

        if (title.contains(".")){
            return false;
        }

        if (title.contains("?")){
            return false;
        }

        if (title.contains("...")){
            return false;
        }

        if (title.contains("..")){
            return false;
        }

        if (title.contains("~")){
            return false;
        }

        if (title.contains("—")){
            return false;
        }

        if (title.contains("·")){
            return false;
        }

        if (title.contains("〈")){
            return false;
        }

        if (title.contains("〉")){
            return false;
        }

        if (title.contains("　　")){
            return false;
        }

        if (title.contains("'")){
            return false;
        }

        if (title.contains("\"")){
            return false;
        }

        if(FontUitls.containsEmoji(title)){
            return false;
        }

        title = title.replaceAll("，","， \n");
        title = title.replaceAll(",",", \n");
        title = title.replaceAll("。","。\n");

        title = title.replaceAll("！","！\n");
        title = title.replaceAll("!","!\n");
        title = title.replaceAll("？","？\n");

        title = title.replaceAll("、","、\n");
        title = title.replaceAll("；","；\n");
        title = title.replaceAll(";",";\n");
        title = title.replaceAll("：", "：\n");
        title = title.replaceAll(":", ":\n");

        title = FontUitls.replaceLineBlanks(title);//去掉连续换行

        int old = title.length();
        title = title.replaceAll("\n","");
        if (old - title.length() > 6){
           if (ScreenUtils.getScreenWidth(CustomApplication.mContext) <= 375*ScreenUtils.getScreenDensity(CustomApplication.mContext)){
               return false;
           }
            if(old - title.length() > 8){
                return false;
            }
        }
        */

        return false;//关闭竖排显示
    }

    public static String formatPortDisplay(String title){

        /*
        1.句号、问号、叹号、逗号、顿号、分号和冒号一般占一个字的位置，居左偏下，不出现在一行之首。
        2.引号、括号、书名号的前一半不出现在一行之末，后一半不出现在一行之首。
        3.破折号和省略号都占两个字的位置，中间不能断开。连接号和间隔号一般占一个字的位置。这四种符号上下居中。
        4.着重号、专名号和浪线式书名号标在字的下边，可以随字移行。
        */

        if (CommonUtils.isEmpty(title)){
            return "";
        }

        title = title.replaceAll("，","， \n");
        title = title.replaceAll(",",", \n");
        title = title.replaceAll("。","。\n");

        title = title.replaceAll("！","！\n");
        title = title.replaceAll("!","!\n");
        title = title.replaceAll("？","？\n");

        title = title.replaceAll("、","、\n");
        title = title.replaceAll("；","；\n");
        title = title.replaceAll(";",";\n");
        title = title.replaceAll("：", "：\n");
        title = title.replaceAll(":", ":\n");

        title = FontUitls.replaceLineBlanks(title);//去掉连续换行


        return title;
    }

    /**
     * 判断字符串中是否含有表情
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        boolean isEmoji = false;
        for (int i = 0; i < len; i++) {
            char hs = source.charAt(i);
            if (0xd800 <= hs && hs <= 0xdbff) {
                if (source.length() > 1) {
                    char ls = source.charAt(i + 1);
                    int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
                    if (0x1d000 <= uc && uc <= 0x1f77f) {
                        return true;
                    }
                }
            } else {
                // non surrogate
                if (0x2100 <= hs && hs <= 0x27ff && hs != 0x263b) {
                    return true;
                } else if (0x2B05 <= hs && hs <= 0x2b07) {
                    return true;
                } else if (0x2934 <= hs && hs <= 0x2935) {
                    return true;
                } else if (0x3297 <= hs && hs <= 0x3299) {
                    return true;
                } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d
                        || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c
                        || hs == 0x2b1b || hs == 0x2b50 || hs == 0x231a) {
                    return true;
                }
                if (!isEmoji && source.length() > 1 && i < source.length() - 1) {
                    char ls = source.charAt(i + 1);
                    if (ls == 0x20e3) {
                        return true;
                    }
                }
            }
        }
        return isEmoji;
    }

    /**
     * 将字符串中的连续的多个换行缩减成一个换行
     * @param str  要处理的内容
     * @return	返回的结果
     */
    public static String replaceLineBlanks(String str) {
        String result = "";
        if (str != null) {
            Pattern p = Pattern.compile("(\r?\n(\\s*\r?\n)+)");
            Matcher m = p.matcher(str);
            result = m.replaceAll("\r\n");
        }
        return result;
    }

}
